package MTT::Controller::Alarms;
use Moose;
use namespace::autoclean;

BEGIN {extends 'Catalyst::Controller'; }

=head1 NAME

MTT::Controller::Alarms - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut


=head2 index

=cut


use Data::Dumper;
use POSIX qw(strftime);
use MTT::Libs::Alarm;
use MTT::Libs::Param::Params;
use MTT::Libs::ReportData;
use MTT::Libs::AlarmRun;


sub base : Chained('/') :PathPart('alarms') : CaptureArgs(0) {
    my ( $self, $c ) = @_;

	my @params;
	foreach ( $c->model('DBIC::Param')->search({type => 0}) ) {
		push @params, MTT::Libs::Param::AlarmParam->new(schema => $c->model('DBIC'), id => $_->id);
	}

	$c->stash(
		report_data => MTT::Libs::ReportData->instance->get_data(),
		alarm_params => \@params
	);
}

sub list : Chained('base') : PathPart('list') : Args(0) {
	my ($self, $c) = @_;

	my @alarms;
	foreach ( $c->model('DBIC::Alarm')->all() ) {
		my $alarm = MTT::Libs::Alarm->new(schema => $c->model('DBIC'));
		$alarm->load( $_->id );
		push @alarms, $alarm;
	}

	$c->stash(
		template => 'alarms.tt2',
		alarms => \@alarms
	);
}

sub view: Chained('base') : PathPart('id') : Args(1) {
	my ($self, $c, $id) = @_;

	unless ( $c->check_user_roles('admin') ) {
		$c->detach('/access_denied');
	}

	my $alarm = MTT::Libs::Alarm->new(schema => $c->model('DBIC'));

	if ( $c->req->params->{saveas} ) {
		$alarm->process( $c->req->params );
		$alarm->save();
	}
	else {
		$alarm->load($id);
	}

	if ( $c->req->params->{save} ) {
		$alarm->process( $c->req->params );
		$alarm->save();
	}

	if ( $c->req->params->{run} ) {
		$alarm->process( $c->req->params );
		$alarm->id( $id );
		my $run = MTT::Libs::AlarmRun->new(
			ctx => $c,
			schema_mtt => $c->model('DBIC')
		);

		$run->run($alarm);
		
	}

	my %params = map { $_->name => $_->condition } @{ $alarm->params };
	foreach ( @{ $c->stash->{alarm_params} } ) {
			$_->condition( $params{ $_->name } ) if $params{ $_->name };
	}

	my @switches;
	foreach ( $alarm->object->alarm_switches->all() ) {
		push @switches, $_->id_switch;
	}

	$c->stash->{switch_do} = \@switches;
	$c->stash(
		template => 'alarm.tt2',
		alarm => $alarm,
	);
}

sub create: Chained('base') : PathPart('new') : Args(0) {
	my ($self, $c) = @_;

	my $alarm = MTT::Libs::Alarm->new({schema => $c->model('DBIC')});
	
	$c->stash(
		template => 'alarm.tt2',
		alarm => $alarm
	);

	if ( $c->req->method eq 'POST' ) {
		$alarm->process( $c->req->params );

		if ( $c->req->params->{run} ) {
			my $run = MTT::Libs::AlarmRun->new(
				ctx => $c,
				schema_mtt => $c->model('DBIC')
			);

			$run->run();
		}
		else {

			$alarm->save();
			$c->response->redirect('/alarms/id/' . $alarm->id);
		}
	}
}

sub remove : Chained('base') : PathPart('delete') : Args(1) {
	my ($self, $c, $id) = @_;
	
	my $alarm = MTT::Libs::Alarm->new(schema => $c->model('DBIC'));
	$alarm->load($id);
	$alarm->remove();

	$c->response->redirect('/alarms/list');
}

sub status_change: Chained(base) : PathPart('status') : Args(2) {
	my ($self, $c, $id, $status) = @_;

	my $alarm = MTT::Libs::Alarm->new(schema => $c->model('DBIC'));
	$alarm->load($id);

	my $st = $status eq 'active' ? 1 : $status eq 'stop' ? 0 : undef;
	if ( defined $st ) {
		$alarm->status( $st );
		$alarm->date_run( strftime('%Y-%m-%d %H:%M:00', localtime) );
		$alarm->object->update();
	}

	$c->response->redirect('/alarms/list');
}
=head1 AUTHOR

A clever guy

=head1 LICENSE

This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

__PACKAGE__->meta->make_immutable;

1;
